package org.apache.http.message;

import java.util.ArrayList;
import java.util.BitSet;
import java.util.List;
import org.apache.http.HeaderElement;
import org.apache.http.NameValuePair;
import org.apache.http.a.a;
import org.apache.http.annotation.Immutable;
import org.apache.http.d;

@Immutable
public class e implements HeaderValueParser {
    @Deprecated
    public static final e a = new e();
    public static final e b = new e();
    private static final BitSet c = k.a(61, 59, 44);
    private static final BitSet d = k.a(59, 44);
    private final k e = k.a;

    public static HeaderElement[] a(String str, HeaderValueParser headerValueParser) throws d {
        a.a((Object) str, "Value");
        org.apache.http.a.d dVar = new org.apache.http.a.d(str.length());
        dVar.a(str);
        j jVar = new j(0, str.length());
        if (headerValueParser == null) {
            headerValueParser = b;
        }
        return headerValueParser.parseElements(dVar, jVar);
    }

    protected HeaderElement a(String str, String str2, NameValuePair[] nameValuePairArr) {
        return new c(str, str2, nameValuePairArr);
    }

    protected NameValuePair a(String str, String str2) {
        return new h(str, str2);
    }

    public HeaderElement[] parseElements(org.apache.http.a.d dVar, j jVar) {
        a.a((Object) dVar, "Char array buffer");
        a.a((Object) jVar, "Parser cursor");
        List arrayList = new ArrayList();
        while (!jVar.c()) {
            HeaderElement parseHeaderElement = parseHeaderElement(dVar, jVar);
            if (parseHeaderElement.getName().length() != 0 || parseHeaderElement.getValue() != null) {
                arrayList.add(parseHeaderElement);
            }
        }
        return (HeaderElement[]) arrayList.toArray(new HeaderElement[arrayList.size()]);
    }

    public HeaderElement parseHeaderElement(org.apache.http.a.d dVar, j jVar) {
        a.a((Object) dVar, "Char array buffer");
        a.a((Object) jVar, "Parser cursor");
        NameValuePair parseNameValuePair = parseNameValuePair(dVar, jVar);
        NameValuePair[] nameValuePairArr = null;
        if (!(jVar.c() || dVar.charAt(jVar.b() - 1) == ',')) {
            nameValuePairArr = parseParameters(dVar, jVar);
        }
        return a(parseNameValuePair.getName(), parseNameValuePair.getValue(), nameValuePairArr);
    }

    public NameValuePair parseNameValuePair(org.apache.http.a.d dVar, j jVar) {
        a.a((Object) dVar, "Char array buffer");
        a.a((Object) jVar, "Parser cursor");
        String a = this.e.a(dVar, jVar, c);
        if (jVar.c()) {
            return new h(a, null);
        }
        char charAt = dVar.charAt(jVar.b());
        jVar.a(jVar.b() + 1);
        if (charAt != '=') {
            return a(a, null);
        }
        String b = this.e.b(dVar, jVar, d);
        if (!jVar.c()) {
            jVar.a(jVar.b() + 1);
        }
        return a(a, b);
    }

    public NameValuePair[] parseParameters(org.apache.http.a.d dVar, j jVar) {
        a.a((Object) dVar, "Char array buffer");
        a.a((Object) jVar, "Parser cursor");
        this.e.a(dVar, jVar);
        List arrayList = new ArrayList();
        while (!jVar.c()) {
            arrayList.add(parseNameValuePair(dVar, jVar));
            if (dVar.charAt(jVar.b() - 1) == ',') {
                break;
            }
        }
        return (NameValuePair[]) arrayList.toArray(new NameValuePair[arrayList.size()]);
    }
}
